#include "MonitoringMain.h"
#include "ui_MonitoringMain.h"
#include <QDebug>
MonitoringMain::MonitoringMain(QWidget *parent,const QString&username) :
    QWidget(parent),
    ui(new Ui::MonitoringMain),
    userSqlite(MonitoringUserSqlite::getInstance())
{
    ui->setupUi(this);
    this->username=username;
    qDebug()<<"==="<<username;
     MonitoringTool::setStlyeSheet(":/imgs/btn.css");
    connect(SnaillTcpClient::instance(), &SnaillTcpClient::dataReceived, this, &MonitoringMain::handleDataReceived);

    if (SnaillTcpClient::instance()->sendJsonData(MonitoringTool::getUserJson("main",username,"","",""))) {
          qDebug() << "Login data sent successfully";
    } else {

    }
    //    p_model=userSqlite.getSqLite(username);

    connect(ui->pushButton_14,&QPushButton::clicked,this,[=](){
        qDebug()<<"aaa";
        ui->stackedWidget->setCurrentIndex(0);
    });
    connect(ui->pushButton_15,&QPushButton::clicked,this,[=](){
        ui->stackedWidget->setCurrentIndex(1);
    });
    connect(ui->pushButton_16,&QPushButton::clicked,this,[=](){
        ui->stackedWidget->setCurrentIndex(2);
    });
    connect(ui->pushButton_17,&QPushButton::clicked,this,[=](){
        ui->stackedWidget->setCurrentIndex(3);
    });
    connect(ui->pushButton_18,&QPushButton::clicked,this,[=](){
        ui->stackedWidget->setCurrentIndex(4);
    });
    connect(ui->pushButton_19,&QPushButton::clicked,this,[=](){
        ui->stackedWidget->setCurrentIndex(5);
    });
    connect(ui->pushButton_20,&QPushButton::clicked,this,[=](){
        ui->stackedWidget->setCurrentIndex(6);
    });


    timer=new QTimer(this);
    connect(timer, &QTimer::timeout, this,[=](){
        ui->label->clear();
    });
//    connect(p_msg,&MonitiringUserMsg::changeTipsClick,this,[=](const QString&str){
//        //timer->start(3000);
//        qDebug()<<"main结果"<<str;
//       // ui->label->setText(str);
//    });
}
void MonitoringMain::handleDataReceived(const QByteArray &data)
{
    qDebug()<<"main====="<<data;
    QJsonDocument jsonDoc = QJsonDocument::fromJson(data);
    QJsonObject jsonObj = jsonDoc.object();
    QString action = jsonObj["action"].toString();
    if(action=="main"){
        qDebug()<<"main+++++"<<data;
        int id =  jsonObj["id"].toInt();
        QString name =  jsonObj["username"].toString();
        QString password = jsonObj["password"].toString();
        QString email =  jsonObj["email"].toString();
        QString role =  jsonObj["role"].toString();
        QString created_at =  jsonObj["created_at"].toString();
        QString updated_at =  jsonObj["updated_at"].toString();
        qDebug()<<"==="<<id;
        p_model=new userModel(id,name,email,role,created_at,updated_at,password);

        if(p_model->role.toInt()==1){
            p_msg=new MonitiringUserMsg(this,p_model);
            p_add=new MonitoringDeviceAdd(this,p_model);
            p_delete=new MonitoringDeviceDelete(this,p_model);
            p_group=new DeviceGroup(this);
            p_history=new MonitoringHistoryData(this,p_model);
            p_warnRecode=new MonitoringWarnRecode;
            p_log =new monitoringSystemLog;
            ui->stackedWidget->addWidget(p_msg);
            ui->stackedWidget->addWidget(p_add);
            ui->stackedWidget->addWidget(p_delete);
            ui->stackedWidget->addWidget(p_group);
            ui->stackedWidget->addWidget(p_history);
            ui->stackedWidget->addWidget(p_warnRecode);
            ui->stackedWidget->addWidget(p_log);
        }else {
               p_msg=new MonitiringUserMsg(this,p_model);
               p_history=new MonitoringHistoryData(this,p_model);
               p_warnRecode=new MonitoringWarnRecode;
               p_log =new monitoringSystemLog;
               ui->pushButton_15->setText("历史数据");
               ui->pushButton_16->setText("警告记录");
               ui->pushButton_17->setText("系统日志");
               ui->stackedWidget->addWidget(p_msg);
               ui->stackedWidget->addWidget(p_history);
               ui->stackedWidget->addWidget(p_warnRecode);
               ui->stackedWidget->addWidget(p_log);
               ui->pushButton_18->setVisible(false);
               ui->pushButton_19->setVisible(false);
               ui->pushButton_20->setVisible(false);
        }
       ui->stackedWidget->setCurrentWidget(p_msg);

    }



}
MonitoringMain::~MonitoringMain()
{
    delete ui;
    delete timer;
}
